//
//  OPMovieDetailsHeaderView.m
//  OnePerson
//
//  Created by xiaofulon on 16/6/12.
//  Copyright © 2016年 BWP. All rights reserved.
//

#import "OPMovieDetailsHeaderView.h"
#import "MZTimerLabel.h"
#import "OPScroeView.h"
#import "OPMovieDetails.h"
#import "OPMovieListItem.h"

@interface OPMovieDetailsHeaderView ()
/**背景view*/
@property (strong, nonatomic) UIImageView *coverView;
/***/
@property (strong, nonatomic) UIImageView *shadowView;
/**分数View*/
@property (strong, nonatomic) OPScroeView *scoreView;
/**即将上映label*/
@property (strong, nonatomic) UILabel *comingSoonLabel;
/**倒计时label*/
@property (strong, nonatomic) MZTimerLabel *timerLabel;

@end

@implementation OPMovieDetailsHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    
    if (self) {
        [self setupViews];
    }
    
    return self;
}


#pragma mark - Private Method

- (void)setupViews {
    // 如果有背景view,则返回
    if (_coverView) {
        return;
    }
    // 设置cell背景和cell的背景view的背景颜色
    self.backgroundColor = [UIColor whiteColor];
    //    self.coverView.backgroundColor = [UIColor whiteColor];
    // 穿件设置背景view
    _coverView = ({
        UIImageView *imageView = [UIImageView new];
        imageView.backgroundColor = [UIColor whiteColor];
        // 将背景View添加到cell
        [self addSubview:imageView];
        // 使背景View的四边和cell的contenView间距0
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        imageView;
    });
    // 创建分数,
    _scoreView = ({
        // 创建分数view
        OPScroeView *view = [OPScroeView new];
        [self addSubview:view];
        // 设置分数约束,利底部和右边有10 的间距
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.bottom.equalTo(self).offset(-10);
        }];
        
        view;
    });
    // 创建还有多久上映label
    _comingSoonLabel = ({
        UILabel *label = [UILabel new];
        label.text = @"即将上映";
        label.textColor = [UIColor colorWithWhite:85 / 255.0 alpha:1];// #555555
        label.font = FontWithSize(12);
        [self addSubview:label];
        // 设置约束,距离contentView底部和右边6的间距
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.bottom.equalTo(self).offset(-6);
        }];
        // 设置隐藏
        label.hidden = YES;
        
        label;
    });
    // 设置距离公布分数事件的Label
    _timerLabel = ({
        // 创建label显示时间
        MZTimerLabel *label = [[MZTimerLabel alloc] initWithTimerType:MZTimerLabelTypeTimer];
        // 设置字体颜色和大小
        label.textColor = [UIColor colorWithWhite:85 / 255.0 alpha:1];// #555555
        label.font = FontWithSize(12);
        label.text = @"00:00:00";
        // 显示时间格式
        label.timeFormat = @"距离公布分数还剩：HH:mm:ss";
        [self addSubview:label];
        // 设置约束,距离contentView底部和右边6的间距
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.bottom.equalTo(self).offset(-6);
        }];
        // 设置为隐藏
        label.hidden = YES;
        
        label;
    });
}

#pragma mark - Public Method

- (void)configureViewWithMovieDetails:(OPMovieDetails *)movieListItem {
    // 拼接占位图片的url
    NSString *placeholderImageName = [NSString stringWithFormat:@"movieList_placeholder_%ld", [movieListItem.movieId integerValue] % 12];
    // 设置背景view图片
    [_coverView mlb_sd_setImageWithURL:movieListItem.detailCover placeholderImageName:placeholderImageName];
    // 检查模型的分数是否为空,为空则还未公布分数
    if (IsStringEmpty(movieListItem.score)) {
        // 背景view隐藏
        _scoreView.hidden = YES;
        // 计算公布分数时间
        NSTimeInterval scoreDiffTimeInterval = [MLBUtilities diffTimeIntervalSinceNowToDateString:movieListItem.scoreTime];
        // 如果时间小于24小时
        if (scoreDiffTimeInterval < 24 * 60 * 60) {
            // 首映label隐藏
            _comingSoonLabel.hidden = YES;
            // 公布分数时间label显示
            _timerLabel.hidden = NO;
            // 让时间计数
            [_timerLabel setCountDownTime:scoreDiffTimeInterval];
            //开启计数
            [_timerLabel start];
        } else {
            // 显示即将上映label
            _comingSoonLabel.hidden = NO;
        }
    } else {// 如果有分数
        //显示分数view
        _scoreView.hidden = NO;
        //隐藏即将上映Label
        _comingSoonLabel.hidden = YES;
        // 隐藏分数公布计时
        _timerLabel.hidden = YES;
        // 分数view显示分数
        _scoreView.scoreLabel.text = movieListItem.score;
    }
}

@end
